package com.lcs.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("provider")
@RefreshScope
public class ProviderController
{

    @Value("${server.port}")
    private Integer PORT;
    @Value("${username}")
    private String USERNAME;

    @GetMapping("/getPort")
    @SentinelResource(value="getPort", blockHandler = "error")
    public String getPort()
    {
        try
        {
            TimeUnit.SECONDS.sleep(0);
        } catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
        System.out.println("被调用了");
        return USERNAME+PORT;
    }
    public String error(BlockException e)
    {
        return "小伙子,你访问的不是时候啊!";
    }
}
